package com.qiyi.player.wonder.plugins.share.view.parts
{
   import flash.display.Sprite;
   import com.iqiyi.components.videoshare.VideoCutBackGround;
   import com.iqiyi.components.videoshare.VideoCutRestrictPlay;
   import com.iqiyi.components.videoshare.VideoCutPlayed;
   import com.iqiyi.components.videoshare.VideoCutPausePoint;
   import com.iqiyi.components.videoshare.VideoCutHeadFlag;
   import com.iqiyi.components.videoshare.VideoCutTileFlag;
   import com.qiyi.player.wonder.common.ui.FastCreator;
   import flash.text.TextField;
   import flash.events.MouseEvent;
   import flash.utils.clearTimeout;
   import flash.utils.setTimeout;
   
   public class ShareVideoCut extends Sprite
   {
      
      public function ShareVideoCut(param1:Number, param2:Number) {
         super();
         this._curTotalDuration = param2;
         this._cutInTime = param1;
         this.addChildren();
      }
      
      private var _videoCutBackGround:VideoCutBackGround;
      
      private var _videoCutRestrictPlay:VideoCutRestrictPlay;
      
      private var _videoCutPlayed:VideoCutPlayed;
      
      private var _videoCutPausePoint:VideoCutPausePoint;
      
      private var _videoCutHeadFlag:VideoCutHeadFlag;
      
      private var _videoCutTileFlag:VideoCutTileFlag;
      
      private var _videoCutHeadTime:ShareVideoCutTime;
      
      private var _videoCutTileTime:ShareVideoCutTime;
      
      private var _headLocation:int = 0;
      
      private var _tileLocation:int = 0;
      
      private var _defaultShareDuration:uint = 0;
      
      private var _leastShareDuration:uint = 0;
      
      private var _mouseHeadDistance:int = 0;
      
      private var _cutInTime:Number;
      
      private var _curTotalDuration:Number;
      
      private var _totalWidth:uint = 320;
      
      private var _shareStartTime:Number;
      
      private var _shareEndTime:Number;
      
      private var _updateTimeTimeout:Number;
      
      private var _deselectState:String = "none";
      
      public function get deselectState() : String {
         return this._deselectState;
      }
      
      private function addChildren() : void {
         this._videoCutBackGround = new VideoCutBackGround();
         this._videoCutBackGround.x = 0;
         this._videoCutBackGround.y = 0;
         this._videoCutBackGround.name = "videoCutBackGround";
         addChild(this._videoCutBackGround);
         this._videoCutRestrictPlay = new VideoCutRestrictPlay();
         this._videoCutRestrictPlay.x = 0;
         this._videoCutRestrictPlay.y = 0;
         this._videoCutRestrictPlay.width = 1;
         this._videoCutRestrictPlay.buttonMode = true;
         this._videoCutRestrictPlay.name = "videoCutRestrictPlay";
         addChild(this._videoCutRestrictPlay);
         this.listenVideoCutRestrictPlay();
         this._videoCutPlayed = new VideoCutPlayed();
         this._videoCutPlayed.x = 0;
         this._videoCutPlayed.y = 0;
         this._videoCutPlayed.width = 300;
         this._videoCutPlayed.buttonMode = true;
         this._videoCutPlayed.name = "videoCutPlayed";
         addChild(this._videoCutPlayed);
         this.listenVideoCutPlayed();
         this._videoCutPausePoint = new VideoCutPausePoint();
         this._videoCutPausePoint.x = 0;
         this._videoCutPausePoint.y = 0;
         addChild(this._videoCutPausePoint);
         this._videoCutHeadFlag = new VideoCutHeadFlag();
         this._videoCutHeadFlag.x = 0;
         this._videoCutHeadFlag.y = -4;
         this._videoCutHeadFlag.buttonMode = true;
         addChild(this._videoCutHeadFlag);
         var _loc1_:TextField = FastCreator.createLabel("开始时间",13421772);
         _loc1_.x = 0;
         _loc1_.y = 23;
         addChild(_loc1_);
         this._videoCutHeadTime = new ShareVideoCutTime(this._curTotalDuration);
         this._videoCutHeadTime.x = 59;
         this._videoCutHeadTime.y = 23;
         addChild(this._videoCutHeadTime);
         this.listenVideoCutHeadFlag();
         this._videoCutTileFlag = new VideoCutTileFlag();
         this._videoCutTileFlag.x = 0;
         this._videoCutTileFlag.y = -4;
         this._videoCutTileFlag.buttonMode = true;
         addChild(this._videoCutTileFlag);
         var _loc2_:TextField = FastCreator.createLabel("结束时间",13421772);
         _loc2_.x = 169;
         _loc2_.y = 23;
         addChild(_loc2_);
         this._videoCutTileTime = new ShareVideoCutTime(this._curTotalDuration);
         this._videoCutTileTime.x = 228;
         this._videoCutTileTime.y = 23;
         addChild(this._videoCutTileTime);
         this.listenVideoCutTileFlag();
         this.initBackTime(this._cutInTime);
      }
      
      private function listenVideoCutPlayed() : void {
         this._videoCutPlayed.addEventListener(MouseEvent.MOUSE_DOWN,this.seekShare,false,0,true);
      }
      
      private function stopListenVideoCutPlayed() : void {
         this._videoCutPlayed.removeEventListener(MouseEvent.MOUSE_DOWN,this.seekShare);
      }
      
      private function listenVideoCutRestrictPlay() : void {
         this._videoCutRestrictPlay.addEventListener(MouseEvent.MOUSE_DOWN,this.seekShare,false,0,true);
      }
      
      private function stopListenVideoCutRestrictPlay() : void {
         this._videoCutRestrictPlay.removeEventListener(MouseEvent.MOUSE_DOWN,this.seekShare);
      }
      
      private function listenVideoCutHeadFlag() : void {
         this._videoCutHeadFlag.addEventListener(MouseEvent.MOUSE_DOWN,this.headFlagDownHandler,false,0,true);
         this._videoCutHeadTime.addEventListener(VideoShareEvent.Evt_TryCutTimeChange,this.headTryCutTimeChange,false,0,true);
         this._videoCutHeadTime.addEventListener(VideoShareEvent.Evt_TryCutTimeStart,this.headTryCutTimeStart,false,0,true);
      }
      
      private function stopListenVideoCutHeadFlag() : void {
         this._videoCutHeadFlag.removeEventListener(MouseEvent.MOUSE_DOWN,this.headFlagDownHandler);
         this._videoCutHeadTime.removeEventListener(VideoShareEvent.Evt_TryCutTimeChange,this.headTryCutTimeChange);
         this._videoCutHeadTime.removeEventListener(VideoShareEvent.Evt_TryCutTimeStart,this.headTryCutTimeStart);
      }
      
      private function listenVideoCutTileFlag() : void {
         this._videoCutTileFlag.addEventListener(MouseEvent.MOUSE_DOWN,this.tileFlagDownHandler,false,0,true);
         this._videoCutTileTime.addEventListener(VideoShareEvent.Evt_TryCutTimeChange,this.tileTryCutTimeChange,false,0,true);
         this._videoCutTileTime.addEventListener(VideoShareEvent.Evt_TryCutTimeStart,this.tileTryCutTimeStart,false,0,true);
      }
      
      private function stopListenVideoCutTileFlag() : void {
         this._videoCutTileFlag.removeEventListener(MouseEvent.MOUSE_DOWN,this.tileFlagDownHandler);
         this._videoCutTileTime.removeEventListener(VideoShareEvent.Evt_TryCutTimeChange,this.tileTryCutTimeChange);
         this._videoCutTileTime.removeEventListener(VideoShareEvent.Evt_TryCutTimeStart,this.tileTryCutTimeStart);
      }
      
      private function seekShare(param1:MouseEvent) : void {
         this.showVideoCutPlayed();
         this._videoCutPlayed.width = mouseX - this._videoCutHeadFlag.x;
         var _loc2_:Number = this.getTimeFromLocation(mouseX);
         dispatchEvent(new VideoShareEvent(VideoShareEvent.Evt_ShareMiddleUp,_loc2_));
      }
      
      private function headFlagDownHandler(param1:MouseEvent) : void {
         swapChildren(this._videoCutTileFlag,this._videoCutHeadFlag);
         this.selectHeadFlag();
         this.clearPlayed();
         this._mouseHeadDistance = mouseX - this._videoCutHeadFlag.x;
         stage.addEventListener(MouseEvent.MOUSE_MOVE,this.headFlagONDrag);
         stage.addEventListener(MouseEvent.MOUSE_UP,this.headFlagDragUp);
         dispatchEvent(new VideoShareEvent(VideoShareEvent.Evt_ShareChange));
      }
      
      private function headFlagONDrag(param1:MouseEvent) : void {
         this._headLocation = mouseX - this._mouseHeadDistance;
         if(this._headLocation > this._videoCutTileFlag.x - this._leastShareDuration)
         {
            this._headLocation = this._videoCutTileFlag.x - this._leastShareDuration;
         }
         if(this._headLocation < 0)
         {
            this._headLocation = 0;
         }
         this._videoCutHeadFlag.x = this._headLocation;
         this.updateHeadFlagTime();
         if(this._videoCutTileFlag.x - this._videoCutHeadFlag.x < this._defaultShareDuration)
         {
            this._videoCutRestrictPlay.width = this._videoCutTileFlag.x - this._videoCutHeadFlag.x;
         }
         else
         {
            this._videoCutRestrictPlay.width = this._defaultShareDuration;
         }
         this._videoCutRestrictPlay.x = this._videoCutHeadFlag.x;
         this._videoCutPlayed.x = this._videoCutHeadFlag.x;
      }
      
      private function headFlagDragUp(param1:MouseEvent) : void {
         stage.removeEventListener(MouseEvent.MOUSE_MOVE,this.headFlagONDrag);
         stage.removeEventListener(MouseEvent.MOUSE_UP,this.headFlagDragUp);
         this.updateShareTime();
         dispatchEvent(new VideoShareEvent(VideoShareEvent.Evt_ShareHeadUp,this.shareStartTime));
      }
      
      private function headTryCutTimeStart(param1:VideoShareEvent) : void {
         this.selectHeadFlag();
         this.clearPlayed();
         dispatchEvent(new VideoShareEvent(VideoShareEvent.Evt_ShareChange));
      }
      
      private function headTryCutTimeChange(param1:VideoShareEvent) : void {
         this._headLocation = this.getLengthFromDuration(this._videoCutHeadTime.curTime);
         if(this._headLocation >= this._videoCutTileFlag.x - this._leastShareDuration)
         {
            this._headLocation = this._videoCutTileFlag.x - this._leastShareDuration;
            this._videoCutHeadTime.setTime(this.getTimeFromLocation(this._headLocation));
         }
         this._videoCutHeadFlag.x = this._headLocation;
         if(this._videoCutTileFlag.x - this._videoCutHeadFlag.x < this._defaultShareDuration)
         {
            this._videoCutRestrictPlay.width = this._videoCutTileFlag.x - this._videoCutHeadFlag.x;
         }
         else
         {
            this._videoCutRestrictPlay.width = this._defaultShareDuration;
         }
         this._videoCutRestrictPlay.x = this._videoCutHeadFlag.x;
         this._videoCutPlayed.x = this._videoCutHeadFlag.x;
         if(param1.tryTimeOK)
         {
            this.updateShareTime();
            dispatchEvent(new VideoShareEvent(VideoShareEvent.Evt_ShareHeadUp,this.shareStartTime));
         }
      }
      
      private function tileFlagDownHandler(param1:MouseEvent) : void {
         swapChildren(this._videoCutTileFlag,this._videoCutHeadFlag);
         this.selectTileFlag();
         this.clearPlayed();
         this._mouseHeadDistance = mouseX - this._videoCutTileFlag.x;
         stage.addEventListener(MouseEvent.MOUSE_MOVE,this.tileFlagONDrag);
         stage.addEventListener(MouseEvent.MOUSE_UP,this.tileFlagDragUp);
         dispatchEvent(new VideoShareEvent(VideoShareEvent.Evt_ShareChange));
      }
      
      private function tileFlagONDrag(param1:MouseEvent) : void {
         this._tileLocation = mouseX - this._mouseHeadDistance;
         if(this._tileLocation > this._totalWidth)
         {
            this._tileLocation = this._totalWidth;
         }
         if(this._tileLocation < this._videoCutHeadFlag.x + this._leastShareDuration)
         {
            this._tileLocation = this._videoCutHeadFlag.x + this._leastShareDuration;
         }
         this._videoCutTileFlag.x = this._tileLocation;
         this.updateTileFlagTime();
         if(this._videoCutTileFlag.x - this._videoCutHeadFlag.x < this._defaultShareDuration)
         {
            this._videoCutRestrictPlay.width = this._videoCutTileFlag.x - this._videoCutHeadFlag.x;
         }
         else
         {
            this._videoCutRestrictPlay.width = this._defaultShareDuration;
         }
      }
      
      private function tileFlagDragUp(param1:MouseEvent) : void {
         stage.removeEventListener(MouseEvent.MOUSE_MOVE,this.tileFlagONDrag);
         stage.removeEventListener(MouseEvent.MOUSE_UP,this.tileFlagDragUp);
         this.updateShareTime();
         dispatchEvent(new VideoShareEvent(VideoShareEvent.Evt_ShareTileUp,this.shareEndTime));
      }
      
      private function tileTryCutTimeStart(param1:VideoShareEvent) : void {
         this.selectTileFlag();
         this.clearPlayed();
         dispatchEvent(new VideoShareEvent(VideoShareEvent.Evt_ShareChange));
      }
      
      private function tileTryCutTimeChange(param1:VideoShareEvent) : void {
         this._tileLocation = this.getLengthFromDuration(this._videoCutTileTime.curTime);
         if(this._tileLocation >= this._totalWidth)
         {
            this._tileLocation = this._totalWidth;
         }
         if(this._tileLocation < this._videoCutHeadFlag.x + this._leastShareDuration)
         {
            this._tileLocation = this._videoCutHeadFlag.x + this._leastShareDuration;
            if(this._curTotalDuration >= 3600000)
            {
               this._videoCutTileTime.setTime(this.getTimeFromLocation(this._tileLocation));
            }
            else
            {
               this._videoCutTileTime.setTime(Math.floor(this.getTimeFromLocation(this._tileLocation)));
            }
         }
         this._videoCutTileFlag.x = this._tileLocation;
         if(this._videoCutTileFlag.x - this._videoCutHeadFlag.x < this._defaultShareDuration)
         {
            this._videoCutRestrictPlay.width = this._videoCutTileFlag.x - this._videoCutHeadFlag.x;
         }
         else
         {
            this._videoCutRestrictPlay.width = this._defaultShareDuration;
         }
         if(param1.tryTimeOK)
         {
            this.updateShareTime();
            dispatchEvent(new VideoShareEvent(VideoShareEvent.Evt_ShareTileUp,this.shareEndTime));
         }
      }
      
      private function initBackTime(param1:Number) : void {
         this._defaultShareDuration = this._totalWidth;
         if(this._curTotalDuration < 120 * 1000)
         {
            this._leastShareDuration = this.getLengthFromDuration(10 * 1000);
         }
         else
         {
            this._leastShareDuration = this.getLengthFromDuration(60 * 1000);
         }
         this._videoCutPausePoint.x = this.getLocationFromTime(param1);
         this._videoCutPlayed.width = this.getLocationFromTime(param1) - this._videoCutPlayed.x;
         this.selectHeadFlag();
         this._videoCutHeadFlag.x = 0;
         this.updateHeadFlagTime();
         this._videoCutRestrictPlay.x = this._videoCutHeadFlag.x;
         this._videoCutRestrictPlay.width = this._defaultShareDuration;
         this._videoCutTileFlag.x = this._videoCutRestrictPlay.x + this._videoCutRestrictPlay.width;
         this.updateTileFlagTime();
         this.clearPlayed();
         if(this._updateTimeTimeout)
         {
            clearTimeout(this._updateTimeTimeout);
         }
         this._updateTimeTimeout = setTimeout(this.updateShareTime,500);
      }
      
      public function selectHeadFlag() : void {
         this._deselectState = "head";
      }
      
      public function selectTileFlag() : void {
         this._deselectState = "tile";
      }
      
      public function deselectAll() : void {
         this._deselectState = "none";
      }
      
      private function updateHeadFlagTime() : void {
         this._videoCutHeadTime.setTime(this.getTimeFromLocation(this._videoCutHeadFlag.x));
      }
      
      public function showVideoCutPlayed() : void {
         this._videoCutPlayed.visible = true;
      }
      
      public function hideVideoCutPlayed() : void {
         this._videoCutPlayed.visible = false;
      }
      
      public function clearPlayed() : void {
         this.hideVideoCutPlayed();
         this._videoCutPlayed.x = this._videoCutHeadFlag.x;
         this._videoCutPlayed.width = 0;
      }
      
      private function updateTileFlagTime() : void {
         if(this._curTotalDuration >= 3600000)
         {
            this._videoCutTileTime.setTime(this.getTimeFromLocation(this._videoCutTileFlag.x));
         }
         else
         {
            this._videoCutTileTime.setTime(Math.floor(this.getTimeFromLocation(this._videoCutTileFlag.x)));
         }
      }
      
      private function updateShareTime() : void {
         this.shareStartTime = this._videoCutHeadTime.curTime;
         this.shareEndTime = this._videoCutTileTime.curTime;
         var _loc1_:VideoShareEvent = new VideoShareEvent(VideoShareEvent.Evt_ShareUpdateTime);
         _loc1_.shareStartTime = this.shareStartTime;
         _loc1_.shareStopTime = this.shareEndTime;
         dispatchEvent(_loc1_);
      }
      
      public function updatePlayed(param1:Number) : void {
         if(this._videoCutPlayed.visible)
         {
            this._videoCutPlayed.width = this.getLocationFromTime(param1) - this._videoCutPlayed.x;
            if(this._videoCutPlayed.width > this._videoCutRestrictPlay.width)
            {
               this._videoCutPlayed.width = this._videoCutRestrictPlay.width;
            }
         }
      }
      
      private function getTimeFromLocation(param1:Number) : Number {
         return Math.floor(param1 * this._curTotalDuration / this._totalWidth);
      }
      
      private function getLocationFromTime(param1:Number) : Number {
         return Math.floor(param1 * this._totalWidth / this._curTotalDuration);
      }
      
      private function getLengthFromDuration(param1:Number) : Number {
         return Math.floor(param1 * this._totalWidth / this._curTotalDuration);
      }
      
      private function getDurationFromLength(param1:Number) : Number {
         return Math.floor(param1 * this._curTotalDuration / this._totalWidth);
      }
      
      public function destory() : void {
         this._videoCutHeadTime.destory();
         this._videoCutTileTime.destory();
      }
      
      public function get shareStartTime() : Number {
         return this._shareStartTime;
      }
      
      public function set shareStartTime(param1:Number) : void {
         this._shareStartTime = param1;
      }
      
      public function get shareEndTime() : Number {
         return this._shareEndTime;
      }
      
      public function set shareEndTime(param1:Number) : void {
         this._shareEndTime = param1;
      }
      
      override public function get width() : Number {
         return this._videoCutBackGround.width;
      }
   }
}
